package flirtme.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.ans.flirtme.AlertDialogManager;
import com.ans.flirtme.ConnectionDetector;
import com.ans.flirtme.R;

import flirtme.constants.Constants;
import flirtme.managers.UserManager;
import flirtme.server.LoginRegisterServices;

public class LogInActivity extends Activity {
	static TextView username;
	static TextView password;
	static String serviceSelected;
	static CheckBox  rememberUser;
	AlertDialogManager alert = new AlertDialogManager();

	// Connection detector
	ConnectionDetector cd;

	public static String getUsername() {
		return username.getText().toString();
	}

	public static String getPassword() {
		return password.getText().toString();
	}
	
	public static String getServiceSelected() {
		return serviceSelected;
	}

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_log_in);
		username = (TextView) this.findViewById(R.id.txtUserName);
		password = (TextView) this.findViewById(R.id.txtPwd);
	}

	


	public void onClickLogin(View v) {
		registerDeviceAndCallService(Constants.LOGIN_SERVICE);
	}



	public void onClickRegister(View v) {
		registerDeviceAndCallService(Constants.REGISTER_SERVICE);
	}

	
	public static void showNextPage(final Context context) {
		
		Boolean rememberMe = rememberUser.isChecked();
		UserManager.instance().setRememberSession(rememberMe);
		UserManager.instance().saveCurrentUserPreferences(context);
		final Class<? extends Activity> activityClass;

		if (UserManager.instance().getProfileCreated()) {
			activityClass = MainMenuActivity.class;
		} else {
			activityClass = UserProfileActivity.class;
		}

		Intent intent = new Intent(context, activityClass);
		context.startActivity(intent);
	}

	@Override
	public void onBackPressed() {
		// This will be called either automatically for you on 2.0
		// or later, or by the code above on earlier versions of the
		// platform.
		return;
	}

	public void registerDeviceAndCallService(final String serviceName) {
		cd = new ConnectionDetector(getApplicationContext());

		if (!cd.isConnectingToInternet()) {
			// Internet Connection is not present
			alert.showAlertDialog(LogInActivity.this,
					"Internet Connection Error",
					"Please connect to working Internet connection", false);
			// stop executing code by return
			return;
		}

	
		serviceSelected = serviceName;
		rememberUser = (CheckBox) findViewById(R.id.checkBoxRememberMe);
		
		callLoginOrRegisterService("");
	
		
		

	}

	private void callLoginOrRegisterService(String regId) {
		String username = LogInActivity.getUsername();
		String password = LogInActivity.getPassword();
		String serviceSelected = LogInActivity.getServiceSelected();
		LoginRegisterServices.callServiceSelected(this, serviceSelected,  username, password, regId);
	}
	
	
	
	
}
